package com.naicheng.ivote.parser;

import org.json.JSONException;
import org.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.naicheng.ivote.doman.BaseResponseEntity;

import android.text.TextUtils;

/**
 * json解析抽象基类 服务器返回json数据时通过事先封装的RequetVo具体事先子类回调解析
 * 
 * @author hanguoxin
 * 
 * @param <T>
 */
public class BaseParser<T> {

	/**
	 * 实现类解析方案
	 * 
	 * @param paramString
	 * @return
	 * @throws JSONException
	 */
	// public abstract BaseResponseEntity<T> parseJSON(String paramString)
	// throws JSONException;

	public BaseResponseEntity<T> parserDomain(JSONObject jsonObject,
			Class<T> cls) {
		BaseResponseEntity<T> response = new BaseResponseEntity<T>();
		try {
			int code = jsonObject.optInt("code");
			String message = jsonObject.optString("message");
			String responsetime = jsonObject.optString("responsetime");
			String data = jsonObject.optString("data");
			// 成功
			if (code == 1) {
				response.setState(true);
				if (TextUtils.isEmpty(data)) {
					response.setMsg("网络不给力呀！请重试！");
				} else {
					response.setSingleDomain(JSON.parseObject(data, cls));
				}

			} else {
				response.setState(false);
				response.setMsg(message);
			}

		} catch (Exception e) {
			response.setState(false);
			response.setMsg("网络不给力呀！请重试！");
		}
		return response;
	}

	public BaseResponseEntity<T> parserDomain(String paramString, Class<T> cls) {

		BaseResponseEntity<T> response = new BaseResponseEntity<T>();

		try {
			response = parserDomain(new JSONObject(paramString), cls);
		} catch (JSONException e) {
			response.setState(false);
			response.setMsg("网络不给力呀！请重试！");
		}
		return response;

	}

	/**
	 * 解析数据
	 * 
	 * @param paramString
	 * @param cls
	 * @return
	 */
	public BaseResponseEntity<T> parserCollection(String paramString,
			Class<T> cls) {

		BaseResponseEntity<T> response = new BaseResponseEntity<T>();

		try {
			JSONObject jsonObject = new JSONObject(paramString);
			response = parserCollection(jsonObject, cls);
		} catch (JSONException e) {
			response.setState(false);
			response.setMsg("网络不给力呀！请重试！");
		}
		return response;
	}

	public BaseResponseEntity<T> parserCollection(JSONObject jsonObject,
			Class<T> cls) {
		BaseResponseEntity<T> response = new BaseResponseEntity<T>();
		int code = jsonObject.optInt("code");
		String message = jsonObject.optString("message");
		String responsetime = jsonObject.optString("responsetime");
		String data = jsonObject.optString("data");
		// 成功
		if (code == 1) {
			response.setState(true);
			if (TextUtils.isEmpty(data)) {
				response.setMsg("网络不给力呀！请重试！");
			} else {
				response.setData(JSON.parseArray(data, cls));
			}
		} else {
			response.setState(false);
			response.setMsg(message);
		}
		return response;
	}

	public BaseResponseEntity<T> parserCollectionList(JSONObject jsonObject,
			Class<T> cls) {
		BaseResponseEntity<T> response = new BaseResponseEntity<T>();
		int code = jsonObject.optInt("code");
		String message = jsonObject.optString("message");
		String responsetime = jsonObject.optString("responsetime");
		String data = jsonObject.optString("data");
		JSONObject datalist = null;
		try {
			datalist = new JSONObject(data);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String list = null;
		if(datalist != null){
			list = datalist.optString("list");
		}
		// 成功
		if (code == 1) {
			response.setState(true);
			if (TextUtils.isEmpty(list)) {
				response.setMsg("网络不给力呀！请重试！");
			} else {
				response.setData(JSON.parseArray(list, cls));
			}
		} else {
			response.setState(false);
			response.setMsg(message);
		}
		return response;
	}
	public BaseResponseEntity<String> parserString(String paramString) {

		BaseResponseEntity<String> response = new BaseResponseEntity<String>();

		try {
			JSONObject jsonObject = new JSONObject(paramString);
			parserString(jsonObject);
		} catch (JSONException e) {
			response.setState(false);
			response.setMsg("网络不给力呀！请重试！");
		}

		return response;
	}

	public BaseResponseEntity<String> parserString(JSONObject jsonObject) {

		BaseResponseEntity<String> response = new BaseResponseEntity<String>();
		int code = jsonObject.optInt("code");
		String message = jsonObject.optString("message");
		String responsetime = jsonObject.optString("responsetime");
		String data = jsonObject.optString("data");
		// 成功
		if (code == 1) {
			response.setState(true);
			if (message == null || "".equals(message)) {
				response.setMsg("网络不给力呀！请重试！");
			} else {
				response.setMsg(message);
			}
		} else {
			response.setState(false);
			response.setMsg(message);
		}
		return response;
	}

}
